package com.zqwh.xtool.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hwit.HwitManager;
import com.zhy.autolayout.AutoLayoutActivity;
import com.zqwh.xtool.R;
import com.zqwh.yj.sdk.utils.LogUtils;
import com.zqwh.yj.sdk.utils.StringUtils;
import com.zqwh.yj.sdk.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import static com.zqwh.xtool.ToolsApp.gProfile;
import static com.zqwh.xtool.ToolsApp.gRopenModel;

/**
 * Created by eason.yang on 2019/7/3.
 */

public abstract class BaseActivity extends AutoLayoutActivity {
    protected RelativeLayout rlMask;
    protected LinearLayout rlDialog;
    protected TextView tvDialogTitle;
    protected TextView tvDialogContent;
    protected Button btnDialogConfirm;
    protected Button btnDialogCancle;
    protected Timer timerDetectDoorStatus;
    private int doorStatus;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onWindowFocusChanged(false);
        requestPermissions(99);
        HwitManager.HwitSetSwapTouchXY(3);//2020.12.28 屏幕翻转设置 谭姜维
        HwitManager.HwitSetHWrotation(180);
    }

    protected void unlock() {
        HwitManager.HwitSetIOValue(gProfile.getLOCK_GPIO_LOCK_CTL(), 1);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                HwitManager.HwitSetIOValue(gProfile.getLOCK_GPIO_LOCK_CTL(), 0);
                ToastUtils.showLongToast(BaseActivity.this, "锁已打开");
                LogUtils.logout("锁已打开");
            }
        }, 200);
    }

    protected void lock(){

    }

    protected int _getDoorStatus(){
        return HwitManager.HwitGetIrqIOValue(gProfile.getLOCK_GPIO_DOOR_DETECT());
    }

    protected void startDetectDoorStatus(){
        doorStatus = gProfile.getLOCK_CLOSE_LEVEL();
        timerDetectDoorStatus = new Timer(true);
        timerDetectDoorStatus.schedule(new TimerTask() {
            public void run() {
                int status = _getDoorStatus();
                if (status != doorStatus) {
                    if (status == gProfile.getLOCK_OPEN_LEVEL()) {
                        onDoorOpened();
                    } else {
                        onDoorClosed();
                    }
                }
                doorStatus = status;
            }
        },0, 500);
    }

    protected void cancelDetectDoorStatus(){
        if (timerDetectDoorStatus != null){
            timerDetectDoorStatus.cancel();
            timerDetectDoorStatus = null;
        }
    }

    protected int getDoorStatus(){
        return gRopenModel.getTrigger1();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelDetectDoorStatus();
    }

    protected void onDoorOpened() {
    }

    protected void onDoorClosed() {
    }

    protected void setViewDialog(View.OnClickListener listener){
        setViewDialog();
        setConfirmClickListener(listener);
    }

    protected void setViewDialog(){
        rlMask = findViewById(R.id.rl_mask);
        rlDialog = findViewById(R.id.ll_dialog);
        tvDialogTitle = findViewById(R.id.tv_dialog_title);
        tvDialogContent = findViewById(R.id.tv_dialog_content);
        btnDialogConfirm = findViewById(R.id.btn_dialog_confirm);
        btnDialogCancle = findViewById(R.id.btn_dialog_cancle);
        btnDialogCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideMyDialog();
            }
        });
    }

    protected void showMyDialog(String title, String content, String cancel, String confirm, float maskAlpha, boolean error, View.OnClickListener confirmListener){
        showMyDialog(title, content, cancel, confirm, maskAlpha, error);
        setConfirmClickListener(confirmListener);
    }

    protected void showMyDialog(String title, String content, String cancel, String confirm, float maskAlpha, boolean error, int hideDelay){
        showMyDialog(title, content, cancel, confirm, maskAlpha, error);
        hideMyDialog(hideDelay);
    }

    protected void showMyDialog(String title, String content, String cancel, String confirm, float maskAlpha, boolean error){
        showMyDialog(title, content, cancel, confirm, maskAlpha);
        if (error) {
            tvDialogContent.setTextColor(getResources().getColor(R.color.colorRed));
        }
    }

    protected void showMyDialog(String title, String content, String cancel, String confirm, float maskAlpha){
        rlMask.setAlpha(maskAlpha);
        showMyDialog(title, content, cancel, confirm);
    }

    protected void showMyDialog(String title, String content, String cancel, String confirm){
        rlMask.setVisibility(View.VISIBLE);
        rlDialog.setVisibility(View.VISIBLE);
        tvDialogTitle.setText(title);
        tvDialogContent.setTextColor(getResources().getColor(R.color.colorAccent));
        tvDialogContent.setText(content);
        if (!StringUtils.isEmpty(cancel)){
            btnDialogCancle.setText(cancel);
            btnDialogCancle.setVisibility(View.VISIBLE);
        }else {
            btnDialogCancle.setVisibility(View.GONE);
        }
        if (!StringUtils.isEmpty(confirm)) {
            btnDialogConfirm.setText(confirm);
            btnDialogConfirm.setVisibility(View.VISIBLE);
        }else {
            btnDialogConfirm.setVisibility(View.GONE);
        }
    }

    protected void hideMyDialog(){
        rlMask.setVisibility(View.GONE);
        rlDialog.setVisibility(View.GONE);
    }

    protected void hideMyDialog(int delayed){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hideMyDialog();
            }
        }, delayed);
    }

    protected void setConfirmClickListener(View.OnClickListener listener){
        btnDialogConfirm.setOnClickListener(listener);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }


    // 请求权限
    public void requestPermissions(int requestCode) {
        try {
            if (Build.VERSION.SDK_INT >= 23) {
                ArrayList<String> requestPerssionArr = new ArrayList<>();
                int hasCamrea = checkSelfPermission(Manifest.permission.CAMERA);
                if (hasCamrea != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.CAMERA);
                }

                int hasSdcardRead = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
                if (hasSdcardRead != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.READ_EXTERNAL_STORAGE);
                }

                int hasSdcardWrite = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (hasSdcardWrite != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                }
                // 是否应该显示权限请求
                if (requestPerssionArr.size() >= 1) {
                    String[] requestArray = new String[requestPerssionArr.size()];
                    for (int i = 0; i < requestArray.length; i++) {
                        requestArray[i] = requestPerssionArr.get(i);
                    }
                    requestPermissions(requestArray, requestCode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        boolean flag = false;
        for (int i = 0; i < permissions.length; i++) {
            if (PackageManager.PERMISSION_GRANTED == grantResults[i]) {
                flag = true;
            }
        }
    }
}
